package com.dg.river.core.view.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.dg.river.R;
import com.dg.river.core.util.TextUtil;


/**
 * Created by hy on 2018/10/16
 * Description :  从顶部弹出
 */
public class CustomTopDialog extends Dialog {

    private static CustomTopDialog dialog;

    private static boolean isDismiss = false;

    private static TextView tv_msg;
    /**
     * 点击事件
     */
    private static OnItemClickListener mOnItemClickListener;

    private static View view;

    /**
     * ItemClick的回调接口
     */
    public interface OnItemClickListener {
        void onItemClick(CustomTopDialog dialog);
    }

    /**
     * 设置点击事件
     *
     * @param mOnItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    public CustomTopDialog(@NonNull Context context) {
        super(context);
    }

    public CustomTopDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    /**
     * 弹出自定义Dialog
     *
     * @param context 上下文
     * @return
     */
    public static CustomTopDialog show(Context context, String msg) {
        dialog = new CustomTopDialog(context);
        dialog.setContentView(R.layout.dialog_custom_top);
        // 按返回键是否取消
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.gravity = Gravity.TOP;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(lp);
        //设置该属性，dialog可以铺满屏幕
        dialog.getWindow().setBackgroundDrawable(null);

        tv_msg = dialog.findViewById(R.id.tv_msg);
        tv_msg.setText(TextUtil.isEmptyConvert(msg));

        tv_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(dialog);
                }
            }
        });

        isDismiss = false;
        dialog.show();

        view = dialog.getWindow().findViewById(R.id.bottom_dialog_layout);
        slideToDown(view);
        return dialog;
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (isOutOfBounds(getContext(), event)) {
            if (!isDismiss) {
                slideToUp(view);
                isDismiss = true;
            }
        }
        return false;
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }


    /**
     * 弹出动画
     *
     * @param view
     */
    public static void slideToUp(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, -1.0f);
        slide.setDuration(300);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dialog.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 弹回动画
     *
     * @param view
     */
    public static void slideToDown(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        slide.setDuration(200);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * dismiss
     */
    public void dialogDismiss() {
        if (dialog != null) {
            slideToUp(view);
        }
    }
}
